<script>
	$(document).ready(function(){
		$('.responsive').stacktable({myClass:'stacktable small-only'});
		$( "input.date" ).datepicker({changeMonth: true,
	            dateFormat: 'yy-mm-dd', 
	            numberOfMonths: 1});
        $('#api').change(function(){
        	//$('#offer')
            $('#seach-form').submit();
         });
	})
	
 
</script>
<?php
	//print_r($api);
	/*foreach($offer as $item)
	{
		print_r($item);
		echo'<br><br>';
	} */
?>
<div class="search-box">
	<form id="seach-form" method="post" action="<?php echo base_url();?>">
		<fieldset>
			<fieldset>
				<dl>
					<dt>Api:</dt>
					<dd>
						<select id="api" name="api">
							<option <?php if($old->api==-1) echo "selected";?> value="-1"></option>
							<?php foreach($api as $item){?>
							<option <?php if($item->id==$old->api) echo "selected";?> value="<?php echo $item->id?>"><?php echo $item->name?></option>
							<?php }?>
						</select>
					</dd>
				</dl>
				<dl>
					<dt>Offer</dt>
					<dd>
						<?php
							$date=new DateTime();
								//foreach($offer as $item) 
								//{echo print_r($item);echo '<br><br>';}
							?>
						<select id="offer" name="offer">
							<option <?php if($old->offer==-1) echo "selected";?> value="-1">All</option>
							<?php
								foreach($offer as $item) 
								{
							?>
							<option value="<?php echo $item->Offer->id?>" <?php if($item->Offer->id==$old->offer)echo "selected"?>><?php echo $item->Offer->name?></option>
							<?php }?>
						</select> 
					</dd>
				</dl>
				<dl>
					<dt>From</dt>
					<dd>
						<input name="from" class="date" type="text" value="<?php echo $old->from?>"/>
					</dd>
				</dl>
				
			</fieldset>
			<fieldset>
			<dl>
				<dt>&nbsp;</dt>
				<dd>
				</dd>
			</dl>
				<dl>
					<dt>Type</dt>
					<dd>
						<select name="type">
							<option value="1" <?php if($old->type==1)echo "selected";?>>Hourly</option>
							<option value="2" <?php if($old->type==2)echo "selected";?>>Daily</option>
							<option value="3" <?php if($old->type==3)echo "selected";?>>Monthly</option>
						</select>
					</dd>
				</dl>
				<dl>
					<dt>To</dt>
					<dd>
						<input name="to" class="date" type="text" value="<?php echo $old->to?>"/>
					</dd>
				</dl>
			</fieldset>
		<input type="submit" name="find" value="Search"/>	
		</fieldset>
		
	</form>
</div>
<table class="responsive">
	<thead>
		<tr>
			<td>Date</td>
			<td>Hour</td>
			<td>OfferID</td>
			<td>Offer</td>
			<td>Browser</td>
			<td>Country</td>
			<td>Impr.</td>
			<td>Clicks</td>
			<td>Unique<br/>Clicks</td>
			<td>Conv.</td>
			<td>CR</td>
		</tr>
	</thead>
	
		<tbody>
			<?php 
				foreach($data as $item)
				{
			?>
			
			<tr>
				<td><?php $date =new DateTime($item->Stat->date); echo  $date->format('Y-m-d');?></td>
				<td><?php if($old->type==1) echo $item->Stat->hour; else echo "&nbsp;" ?></td>
				<td><?php echo $item->Stat->offer_id; ?></td>
				<td><?php echo $item->Offer->name; ?></td>
				<td><?php echo $item->Browser->display_name; ?></td>
				<td><?php echo $item->Country->name; ?></td>
				<td><?php echo $item->Stat->impressions; ?></td>
				<td><?php echo $item->Stat->clicks; ?></td>
				<td><?php echo $item->Stat->unique_clicks; ?></td>
				<td><?php echo $item->Stat->conversions; ?></td>
				<?php $n=round($item->Stat->conversions/$item->Stat->clicks,2);?>
				<td <?php if($n>0.05) echo 'style="color:red"';?>><?php if($item->Stat->clicks!=0) echo $n ; else echo '0';?></td>
			</tr>			
			<?php 
				}
			?>
		</tbody>
	
</table>